package com.example.driving.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.driving.base.BaseContext;
import com.example.driving.base.BaseFragment;
import com.example.driving.base.Data;
import com.example.driving.databinding.FragmentSubjectBinding;
import com.example.driving.model.ExamQuestionBean;
import com.example.driving.model.InfoBean;
import com.example.driving.model.UserBean;
import com.example.driving.utils.NetUtils;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class SubjectFragment extends BaseFragment<FragmentSubjectBinding> {

    @Override
    public void onStart() {
        System.out.println("onStart");
        getErrorOrCollect();
        super.onStart();
    }

    private FragmentSubjectBinding mBinding;
    private int subject;
    public static SubjectFragment getInstance(int subject) {
        System.out.println("getInstance--");
        SubjectFragment sf = new SubjectFragment();
        sf.subject = subject;
        return sf;
    }

    @Override
    public FragmentSubjectBinding getViewBinding(LayoutInflater inflater, ViewGroup container) {
        System.out.printf("getViewBinding--");
        mBinding = FragmentSubjectBinding.inflate(inflater, container, false);
        return mBinding;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        System.out.println("initView--");
        getQuestions();

    }

    @Override
    protected void initData() {
//        mBinding.btVideo.setOnClickListener(v -> {
//            startActivity(new Intent(getContext(), SimpleVideoActivity.class));
//        });

        Intent startIntent = new Intent(getContext(), LearnActivity.class);
        // 练习模式
        if (this.subject != 4) {
            mBinding.slPractice.setOnClickListener(v -> {
                startIntent.putExtra("type", "learn");
                startActivity(startIntent);
            });

            // 考试模式
            mBinding.slExam.setOnClickListener(v -> {
                startIntent.putExtra("type", "exam");
                startActivity(startIntent);
            });

            mBinding.tvRetrench.setOnClickListener(v->{
                startIntent.putExtra("type", "500");
                startActivity(startIntent);
            });


            mBinding.slChapter.setOnClickListener(v->{
                startIntent.putExtra("type", "collect");
                startActivity(startIntent);
            });

            mBinding.tvErrCol.setOnClickListener(v->{
                startIntent.putExtra("type", "ErrCol");
                startActivity(startIntent);
            });

            mBinding.slNewBanks.setOnClickListener(view -> {
                startIntent.putExtra("type", "NewBanks");
                startActivity(startIntent);
            });
        } else {
            startIntent.putExtra("type", "NewBanks");
            mBinding.slPractice.setOnClickListener(v -> {
                startActivity(startIntent);
            });

            // 考试模式
            mBinding.slExam.setOnClickListener(v -> {
                startActivity(startIntent);
            });

            mBinding.tvRetrench.setOnClickListener(v->{
                startActivity(startIntent);
            });


            mBinding.slChapter.setOnClickListener(v->{
                startActivity(startIntent);
            });

            mBinding.tvErrCol.setOnClickListener(v->{
                startActivity(startIntent);
            });

            mBinding.slNewBanks.setOnClickListener(view -> {
                startActivity(startIntent);
            });
        }



    }

    @Override
    public void onBaseClick(View v) {

    }

    //技巧练题 题库导入
    public static List<ExamQuestionBean> SkillConnection = new ArrayList<>();

    //科目四 题库导入
    public static List<ExamQuestionBean> ExamQuestions4 = new ArrayList<>();

    //模拟考试 题库导入
    public static List<ExamQuestionBean> practiceTest = new ArrayList<>();

    //500精简 题库导入
    public static List<ExamQuestionBean> SimpQuestion500 = new ArrayList<>();


    /**
     * 题库数据导入
     */
    private void getQuestions() {
        //技巧练题 题库导入
        Call<Data<List<ExamQuestionBean>>> allExamQuestion = NetUtils.get().allExamQuestion(1);

        allExamQuestion.enqueue(new Callback<Data<List<ExamQuestionBean>>>() {
            @Override
            public void onResponse(Call<Data<List<ExamQuestionBean>>> call, Response<Data<List<ExamQuestionBean>>> response) {
                Data<List<ExamQuestionBean>> body = response.body();
                List<ExamQuestionBean> data = body.getData();
                if (data != null) {
                    SkillConnection=data;
                    mBinding.tvIndex.setText(String.valueOf(data.size()));
                }

            }

            @Override
            public void onFailure(Call<Data<List<ExamQuestionBean>>> call, Throwable t) {

            }
        });


        //科目四 题库导入
        Call<Data<List<ExamQuestionBean>>> allExamQuestion4 = NetUtils.get().allExamQuestion(4);

        allExamQuestion4.enqueue(new Callback<Data<List<ExamQuestionBean>>>() {
            @Override
            public void onResponse(Call<Data<List<ExamQuestionBean>>> call, Response<Data<List<ExamQuestionBean>>> response) {
                Data<List<ExamQuestionBean>> body = response.body();
                List<ExamQuestionBean> data = body.getData();
                if (data != null) {
                    ExamQuestions4=data;
                }

            }

            @Override
            public void onFailure(Call<Data<List<ExamQuestionBean>>> call, Throwable t) {

            }
        });


        //模拟考试 题库导入

        Call<Data<List<ExamQuestionBean>>> randExamQuestion100 = NetUtils.get().randExamQuestion(100, BaseContext.getCurrentId(), 1, 1);
        randExamQuestion100.enqueue(new Callback<Data<List<ExamQuestionBean>>>() {
            @Override
            public void onResponse(Call<Data<List<ExamQuestionBean>>> call, Response<Data<List<ExamQuestionBean>>> response) {
                Data<List<ExamQuestionBean>> body = response.body();
                List<ExamQuestionBean> data = body.getData();
                if (data != null) {
                    practiceTest=data;
                }

            }

            @Override
            public void onFailure(Call<Data<List<ExamQuestionBean>>> call, Throwable t) {

            }
        });



        //500题精简题库 题库导入
        Call<Data<List<ExamQuestionBean>>> randExamQuestion500 = NetUtils.get().randExamQuestion(500, BaseContext.getCurrentId(), 1, 1);
        randExamQuestion500.enqueue(new Callback<Data<List<ExamQuestionBean>>>() {
        @Override
        public void onResponse(Call<Data<List<ExamQuestionBean>>> call, Response<Data<List<ExamQuestionBean>>> response) {
            Data<List<ExamQuestionBean>> body = response.body();
            List<ExamQuestionBean> data = body.getData();
            if (data != null) {
                SimpQuestion500=data;
            }

        }

        @Override
        public void onFailure(Call<Data<List<ExamQuestionBean>>> call, Throwable t) {

        }
    });




    }


    //收藏题库
    public static List<ExamQuestionBean> Collects = new ArrayList<>();

    //错题题库
    public static List<ExamQuestionBean> Errors = new ArrayList<>();

    public static InfoBean infoBean =new InfoBean();

    private void getErrorOrCollect(){


        //收藏题库 题库导入
        Call<Data<List<ExamQuestionBean>>> CollectExamQuestion = NetUtils.get().allCollect(BaseContext.getCurrentId());
        CollectExamQuestion.enqueue(new Callback<Data<List<ExamQuestionBean>>>() {
            @Override
            public void onResponse(Call<Data<List<ExamQuestionBean>>> call, Response<Data<List<ExamQuestionBean>>> response) {
                Data<List<ExamQuestionBean>> body = response.body();
                List<ExamQuestionBean> data = body.getData();
                if (data != null) {
                    Collects=data;
                }

            }

            @Override
            public void onFailure(Call<Data<List<ExamQuestionBean>>> call, Throwable t) {

            }
        });


        //错题题库 题库导入
        Call<Data<List<ExamQuestionBean>>> errorExamQuestion = NetUtils.get().ErrorExamQuestion(BaseContext.getCurrentId());
        errorExamQuestion.enqueue(new Callback<Data<List<ExamQuestionBean>>>() {
            @Override
            public void onResponse(Call<Data<List<ExamQuestionBean>>> call, Response<Data<List<ExamQuestionBean>>> response) {
                Data<List<ExamQuestionBean>> body = response.body();
                List<ExamQuestionBean> data = body.getData();
                if (data != null) {
                    Errors=data;
                }

            }

            @Override
            public void onFailure(Call<Data<List<ExamQuestionBean>>> call, Throwable t) {

            }
        });

        //累计答题考试数据导入
        Call<Data<InfoBean>> info1 = NetUtils.get().info(BaseContext.getCurrentId(), 1);
        info1.enqueue(new Callback<Data<InfoBean>>() {
            @Override
            public void onResponse(Call<Data<InfoBean>> call, Response<Data<InfoBean>> response) {
                Data<InfoBean> body = response.body();
                InfoBean data = body.getData();
                infoBean=data;
                System.out.println("-----info1");
                System.out.println(data.toString());

            }

            @Override
            public void onFailure(Call<Data<InfoBean>> call, Throwable t) {

            }
        });



        Call<Data<InfoBean>> info4 = NetUtils.get().info(BaseContext.getCurrentId(), 4);
        info4.enqueue(new Callback<Data<InfoBean>>() {
            @Override
            public void onResponse(Call<Data<InfoBean>> call, Response<Data<InfoBean>> response) {
                Data<InfoBean> body = response.body();
                InfoBean data = body.getData();
                System.out.println("-----info4");
                System.out.println(data.toString());
            }

            @Override
            public void onFailure(Call<Data<InfoBean>> call, Throwable t) {

            }
        });


    }

}
